package com.lanou.musicproject.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import com.lanou.musicproject.bean.MySong;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ztq on 15/10/27.
 */
public class SongList {


        public static List<MySong> getMusicData(Context context) {
            List<MySong> songList = new ArrayList<MySong>();
            ContentResolver cr = context.getContentResolver();
            if (cr != null) {
                // 获取所有歌曲

                Cursor cursor = cr.query(
                        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,
                        null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
                if (null == cursor) {
                    return null;
                }
                if (cursor.moveToFirst()) {
                    do {
                        MySong m = new MySong();
                        String title = cursor.getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.TITLE));
                        String artist = cursor.getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.ARTIST));
                        if ("<unknown>".equals(artist)) {
                            artist = "未知艺术家";
                        }
                        String album = cursor.getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.ALBUM));
                        long size = cursor.getLong(cursor
                                .getColumnIndex(MediaStore.Audio.Media.SIZE));
                        long duration = cursor.getLong(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DURATION));
                        String url = cursor.getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DATA));
//                        String title = cursor
//                                .getString(cursor
//                                        .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                        String sbr = title.substring(title.length() - 3,
                                title.length());
                        //Log.e("--------------", sbr);
                        if (sbr.equals("mp3")) {
                            m.setTitle(title);
                            m.setArtist(artist);
                            m.setAlbum(album);
                           // m.setSize(size);
                            m.setDuration(duration);
                           // m.setUrl(url);
                            m.setTitle(title);
                            songList.add(m);
                        }
                    } while (cursor.moveToNext());
                }
            }
            return songList;

        }
    }


